<template>
  <div>
    
    <div class="border" v-if="addressInfo.id" @click="$router.push('/addressList')">
        <div>{{addressInfo.username}} {{addressInfo.userphone}}</div>
        <div>{{addressInfo.location}}</div>
    </div>
    <button  v-else  @click="$router.push('/addressList')">选择地址</button>
   <van-swipe-cell v-for="item in list" :key="item.id">
      <van-card
        :num="item.num"
        :price="item.price"
        desc="描述信息"
        :title="item.goodsname"
        class="goods-card"
        :thumb="$pre+item.img"
      />
    </van-swipe-cell>

    <div>
      <span>合计：￥{{countPrice}}</span>
      <button @click="toOrder">确认订单</button>
    </div>
  </div>
</template>

<script>
import { reqAddressList, reqOrderAdd, reqShopList } from "../../http/api";
export default {
  data(){
    return{
      //1地址详情
      addressInfo:{},
      //3.购物车
      list:[],
      countPrice:""

    }
  },
  mounted(){
    //计算总价
    this.countPrice=JSON.parse(localStorage.getItem("order")).countmoney;

    
     // 2.如果有地址，就取地址
    if (this.$route.query.addressId) {
      reqAddressList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.addressId,
      }).then((res) => {
        this.addressInfo = res.data.list[0];
      });
    }
  //5.请求购物车的数据
    let idstr = JSON.parse(localStorage.getItem("order")).idstr; // '8,9,10'
    idstr.split(",").forEach((item) => {
      //item :15
      reqShopList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: item,
      }).then((res) => {
        this.list.push(res.data.list[0]);
      });
    });

  },
  methods:{
    //下单
    toOrder(){
      let json=JSON.parse(localStorage.getItem("order"))
        
      //生成订单
      reqOrderAdd({
        uid:JSON.parse(localStorage.getItem("user")).uid,
        addressid:this.$route.query.addressId,
        ...json
      }).then(res=>{
        this.$router.push("/pay?orderId="+res.data.list.outTradeNo)
      })
    }
  }
  
  
}
</script>

<style scoped>
.border{
  width:90%;
  height: 2rem;
  border:0.01rem solid #ccc;
  margin: 0 auto;
  margin-top: 0.1rem;

}
.border div{
  margin-left:0.1rem;
  line-height: 0.8rem;
}
.van-card{
  margin-top:0.2rem;
}
</style>